package com.gitee.openviducn.inspector.domain.vo.response.openvidu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.gitee.openviducn.inspector.domain.vo.response.BaseResponse;
import com.google.common.collect.Lists;
import io.openvidu.java.client.Connection;
import io.swagger.annotations.ApiModel;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: https://gitee.com/wesleyOne
 * @Date: 03.03 2020
 */
@ApiModel(value = "Connection信息")
@Getter
@Setter
public class ConnectionVO extends BaseResponse {
    private static final long serialVersionUID = -1745307595378464238L;

    private String connectionId;
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date createdAt;
    private String role;
    private String token;
    private String location;
    private String platform;
    private String serverData;
    private String clientData;

    protected List<PublisherVO> publishers;
    protected List<String> subscribers;

    public static ConnectionVO build(Connection con) {
        ConnectionVO c = new ConnectionVO();
        c.setConnectionId(con.getConnectionId());
        c.setCreatedAt(new Date(con.createdAt()));
        c.setRole(con.getRole().name());
        c.setToken(con.getToken());
        c.setClientData(con.getClientData());
        c.setServerData(con.getServerData());
        List<PublisherVO> pubs = Lists.newArrayList();
        con.getPublishers().forEach(p -> pubs.add(PublisherVO.build(p)));
        c.setPublishers(pubs);
        List<String> subs = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(subs)) {
            subs.addAll(con.getSubscribers());
        }
        c.setSubscribers(subs);
        return c;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
